﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;

#endregion


namespace ScrumTable.Common.Data.Tracking
{
    /// <summary>
    /// Provides an unique id generator.
    /// </summary>
    public class IdGenerator
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string IdFormat = "[{0} {1}]";
        private IDictionary<string, int> _idCategories = new Dictionary<string, int>();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Generates a new id and uses the category for the new id.
        /// </summary>
        /// <param name="category">Provides an id category for id creation purposes.</param>
        /// <returns>Returns the new created unique id.</returns>
        public string NewId(string category)
        {
            PreCondition.AssertNotNullOrEmpty(category, "category");

            lock (this)
            {
                if (!_idCategories.ContainsKey(category))
                {
                    _idCategories[category] = 0;
                }

                int currentId = ++_idCategories[category];
                return string.Format(IdFormat, category, currentId);
            }
        }

        /// <summary>
        /// Resets the id generator to its initial state.
        /// </summary>
        public void Reset()
        {
            lock (this)
            {
                _idCategories = new Dictionary<string, int>();
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}